package com.jk.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jk.controller.BaseController;
import com.jk.pojo.PersonTag;
import com.jk.service.PersonTagService;

/**
 * 个人标签管理
 * @author cuiP
 *
 */
@Controller
@RequestMapping("/admin/person_tag/")
public class PersonTagController extends BaseController{

	private static final String JSP_PATH = "/person_tag/";
	
	@Resource
    private	PersonTagService personTagService;
	
	/**
	 * 个人标签管理列表
	 * @param request
	 * @return
	 */
	@RequestMapping("list.do")
	public String list(HttpServletRequest request,ModelMap modelMap){
		List<PersonTag> personTagList = personTagService.queryAll();
		modelMap.put("list", personTagList);
		return JSP_PATH+"list";
	}

	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("add.do")
	public String add(HttpServletRequest request){
		return JSP_PATH+"add";
	}
	
	/**
	 * 保存
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="save.do",method=RequestMethod.POST)
	public Map<String, Object> save(HttpServletRequest request,PersonTag personTag){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			personTagService.save(personTag);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_SAVE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","保存失败!");
		}
		return messageMap;
	}
	
	/**
	 * 跳转到修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request,@RequestParam(required=true,defaultValue="0") Long id,ModelMap modelMap){
		PersonTag personTag = personTagService.queryById(id);
		modelMap.put("personTag", personTag);
		return JSP_PATH+"edit";
	}
	
	/**
	 * 更新
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update.do",method=RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request,PersonTag personTag){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			personTagService.update(personTag);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_UPDATE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","更新失败!");
		}
		return messageMap;
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete/{id}.do",method=RequestMethod.DELETE)
	public Map<String, Object> delete(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			personTagService.deleteById(id);
			//TODO
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_DELETE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}
}
